<?php

namespace Admin\Controller;
use Think\Controller;

class ParameterController extends AdminBasicController{

	public $Parameter = '';
	public $Function = '';
	public $Project = '';

	public function _initialize(){
		$this->checkLogin();
		$this->Function = D('Function');
		$this->Parameter = D('Parameter');
		$this->Project = D('Project');
	}

	/**
	 * 模块列表
	 */
	public function parameterList(){
		$where = array();
		if(isset($_POST['pa_name'])){
			$where['pa_name'] = array('like','%'.$_POST['pa_name'].'%');
		}
		$parameter_list = $this->Parameter->selectParameter($where,'ctime desc',10);

		$this->assign('parameter_list',$parameter_list['list']);
		$this->assign('page',$parameter_list['page']);
		$this->display('Parameter/parameterList');
	}


	public function ajaxModule(){
		$where['p_id'] = $_POST['pid'];
		$module_list = $this->Module->selectModule($where,'','');
		$this->ajaxReturn($module_list,'JSON');
	}

	public function ajaxFun(){
		$where['mo_id'] = $_POST['moid'];
		$function_list = $this->Function->selectFunction($where,'','');
		$this->ajaxReturn($function_list,'JSON');
	}

	/**
	 * 增加模块
	 */
	public function addParameter(){
		if(empty($_POST)){
			$project_list = $this->Project->selectProject('','ctime desc','');
			$function_list = $this->Function->selectFunction('','ctime desc','');
			$this->assign('function_list',$function_list);
			$this->assign('project_list',$project_list);
			$this->display('Parameter/addParameter');
		}else{
			if($data=$this->Parameter->create()){
				$data['ctime'] = time();
				if($this->Parameter->addParameter($data)){
					$this->success('添加模块成功',U('Parameter/parameterList'));
				}else{
					$this->success('添加失败');
				}
			}else{
				$this->error($this->Parameter->getError());
			}
		}
	}

	/**
	 * 编辑模块
	 */
	public function editParameter(){
		if(empty($_POST)){
			$where['pa_id'] = I('get.pa_id');
			$parameter_find = $this->Parameter->findParameter($where);
			$function_list = $this->Function->selectFunction('','ctime desc','');
			$this->assign('function_list',$function_list);
			$this->assign('parameter_find',$parameter_find);
			$this->display('Parameter/editParameter');
		}else{
			if($data=$this->Parameter->create()){
				if($this->Parameter->editParameter(array('pa_id'=>$_POST['pa_id']),$data)){
					$this->success('编辑模块成功',U('Parameter/parameterList'));
				}else{
					$this->success('编辑失败');
				}
			}else{
				$this->error($this->Parameter->getError());
			}
		}
	}


	public function deleteParameter(){
		$this->checkAuth('Parameter','deleteParameter');
		if(empty($_REQUEST['pa_id'])){
			$this->error('您未选择任何操作对象');
		}
		$where['pa_id'] = array('IN',I('request.pa_id'));
		$data['status'] = 9;
		$data['utime'] = time();
		$id = $this->Parameter->editParameter($where,$data);
		if($id){
			$this->success('删除操作成功');
		}else{
			$this->error('删除操作失败');
		}
	}

}


?>